import React, { useEffect, useState } from 'react'

export const CurtDown: React.FC<{ curtDownTime: number }> = ({ curtDownTime }) => {
	function formatTime(ms: number) {
		const s = Math.floor(ms / 1000 % 60)
		const m = Math.floor(ms / 1000 / 60 % 60)
		const h = Math.floor(ms / 1000 / 60 / 60 % 24)
		const d = Math.floor(ms / 1000 / 60 / 60 / 24)
		return `倒计时还剩${d}天${h}时${m}分${s}秒`
	}
	const [time, setTime] = useState(curtDownTime);

	useEffect(() => {
		const timer = setTimeout(() => {
			setTime(time - 1000)
		}, 1000);

		return () => clearTimeout(timer);
	}, [time])
	
	return <div>
		{ formatTime(time) }
	</div>
}